# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

AC_INIT([log4cxx], [0.11.0])
AC_CONFIG_MACRO_DIR([src/m4])
AC_CONFIG_SRCDIR(src/main/cpp/logger.cpp)

# autoconf 2.50 or higher to rebuild aclocal.m4, because the
# AC_CREATE_PREFIX_CONFIG_H macro needs the AS_DIRNAME macro.
AC_PREREQ(2.50)

AC_CANONICAL_HOST
AC_CANONICAL_TARGET

#
# +1 : ? : +1  == new interface that does not break old one
# +1 : ? : 0   == new interface that breaks old one
#  ? : ? : 0   == no new interfaces, but breaks apps
#  ? :+1 : ?   == just some internal changes, nothing breaks but might work
#                 better
# CURRENT : REVISION : AGE
LT_VERSION=10:0:0

AC_SUBST(LT_VERSION)

AH_VERBATIM([HAVE_NO_EXPLICIT_EXPORTS],
[/* no explicit exports */
#define LOG4CXX_EXPORT])

AM_INIT_AUTOMAKE([foreign subdir-objects -Wall])

# Checks for programs
# ----------------------------------------------------------------------------

AM_PROG_AR
AM_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

AC_PROG_CXX([g++ c++ gpp aCC CC cxx cc++ cl FCC KCC RCC xlC_r xlC QCC])

AC_PROG_CXXCPP
AC_LANG(C++)

# CXX fine tuning
case "$host" in
    *-dec-osf*)
        CXXFLAGS="$CXXFLAGS -std strict_ansi_errors"
        ;;
    *)
        ;;
esac

# Doxygen

AC_ARG_ENABLE(doxygen,
        AC_HELP_STRING(--enable-doxygen,
                [enable documentation generation with doxygen (auto)]))

AC_ARG_ENABLE(dot,
        AC_HELP_STRING(--enable-dot,
                [use 'dot' to generate graphs in doxygen (auto)]))

AC_ARG_ENABLE(html-docs,
        AC_HELP_STRING(--enable-html-docs,
                [enable HTML generation with doxygen (yes)]),
        [],
        [enable_html_docs=yes])

AC_ARG_ENABLE(latex-docs,
        AC_HELP_STRING(--enable-latex-docs,
                [enable LaTeX documentation generation with doxygen (no)]),
        [],
        [enable_latex_docs=no])

if test "x$enable_doxygen" = xno; then
        enable_doc=no
else
        AC_PATH_PROG(DOXYGEN, doxygen, , $PATH)
        if test "x$DOXYGEN" = x; then
                        if test "x$enable_doxygen" = xyes; then
                                        AC_MSG_ERROR([could not find doxygen])
                        fi
                        enable_doc=no
        else
                        enable_doc=yes
                        AC_PATH_PROG(DOT, dot, , $PATH)
        fi
fi
AM_CONDITIONAL(DOC, test x$enable_doc = xyes)
AM_CONDITIONAL(LATEX_DOC, test x$enable_latex_docs = xyes)

if test x$DOT = x; then
        if test "x$enable_dot" = xyes; then
                AC_MSG_ERROR([could not find dot])
        fi
        enable_dot=no
else
        enable_dot=yes
fi

manual_dest="manual"
base_dir=`(cd $srcdir && pwd)`

AC_SUBST(enable_dot)
AC_SUBST(enable_html_docs)
AC_SUBST(enable_latex_docs)
AC_SUBST(manual_dest)
AC_SUBST(base_dir)




# Checks header files
# ----------------------------------------------------------------------------
APR_FIND_APR( , , 1, 1)
if test "$apr_found" = "no"; then
  AC_MSG_ERROR(APR could not be located. Please use the --with-apr option.)
fi

CPPFLAGS="$CPPFLAGS `$apr_config --cppflags` `$apr_config --includes`"
APR_LIBS="`$apr_config --link-ld --libs`"
AC_SUBST(APR_LIBS)
LDFLAGS="$LDFLAGS $APR_LIBS"

APR_FIND_APU( , , 1, 1)
if test "$apu_found" = "no"; then
 AC_MSG_ERROR(APR-util could not be located. Please use the --with-apr-util option.)
fi

CPPFLAGS="$CPPFLAGS `$apu_config --includes`"
APU_LIBS="`$apu_config --link-ld --libs`"
AC_SUBST(APU_LIBS)
LDFLAGS="$LDFLAGS $APU_LIBS"

# Checks local idioms
# ----------------------------------------------------------------------------

# for local syslog() function for SyslogAppender
AC_CHECK_FUNCS(mbsrtowcs, [have_mbsrtowcs=yes], [have_mbsrtowcs=no])
if test "$have_mbsrtowcs" = "yes"
then
 AC_SUBST(HAS_MBSRTOWCS, 1)
else
 AC_SUBST(HAS_MBSRTOWCS, 0)
fi

AC_CHECK_FUNCS(wcstombs, [have_wcstombs=yes], [have_wcstombs=no])
if test "$have_wcstombs" = "yes"
then
 AC_SUBST(HAS_WCSTOMBS, 1)
else
 AC_SUBST(HAS_WCSTOMBS, 0)
fi

# for local syslog() function for SyslogAppender
AC_CHECK_FUNCS(syslog, [have_syslog=yes], [have_syslog=no])
if test "$have_syslog" = "yes"
then
 AC_SUBST(HAS_SYSLOG, 1)
else
 AC_SUBST(HAS_SYSLOG, 0)
fi

AC_CHECK_HEADER([locale],have_locale=yes,have_locale=no)
if test "$have_locale" = "yes"
then
 AC_SUBST(HAS_STD_LOCALE, 1)
else
 AC_SUBST(HAS_STD_LOCALE, 0)
fi

AC_MSG_CHECKING([for wchar_t])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
                   #include <string>
                   std::wstring w;])], [have_wchar_t=yes], [have_wchar_t=no])
AC_MSG_RESULT($have_wchar_t)
if test "$have_wchar_t" = "yes"
then
 AC_SUBST(HAS_WCHAR_T, 1)
else
 AC_SUBST(HAS_WCHAR_T, 0)
fi

AC_CHECK_FUNCS(fwide, [have_fwide=yes], [have_fwide=no])
if test "$have_fwide" = "yes"
then
 AC_SUBST(HAS_FWIDE, 1)
else
 AC_SUBST(HAS_FWIDE, 0)
fi


# Checks for libraries
# ----------------------------------------------------------------------------

#for ODBCAppender
AC_MSG_CHECKING(for ODBC support)
AC_ARG_WITH(ODBC,
        AC_HELP_STRING(--with-ODBC, [ODBC support. Accepted arguments :
                unixODBC, iODBC, Microsoft, no (default=no)]),
        [ac_with_odbc=$withval],
        [ac_with_odbc=no])
case "$ac_with_odbc" in
    Microsoft)
        AC_SUBST(HAS_ODBC, 1, ODBC support)
        AC_MSG_RESULT(Microsoft)
        LIBS_ODBC="-lodbc32"
        ;;
    unixODBC)
        AC_MSG_RESULT(unixODBC)
        AC_CHECK_HEADER(sqlext.h,, AC_MSG_ERROR(unixODBC not found !))
        AC_SUBST(HAS_ODBC, 1, ODBC support)
        LIBS_ODBC="-lodbc"
        ;;
    iODBC)
        AC_MSG_RESULT(iODBC)
        AC_CHECK_PROG(IODBC_CONFIG, iodbc-config, yes, no)
        if test "x$IODBC_CONFIG" = "xyes"
        then
                AC_SUBST(HAS_ODBC, 1, ODBC support)
                LIBS_ODBC="`iodbc-config --libs`"
                CPPFLAGS_ODBC="`iodbc-config --cflags`"
        else
                AC_MSG_ERROR(iODBC not found !)
        fi
        ;;
        no)
        AC_SUBST(HAS_ODBC, 0, No ODBC support)
        AC_MSG_RESULT(no)
        ;;
    *)
        AC_MSG_RESULT(???)
        AC_MSG_ERROR(Unknown option : $ac_with_odbc)
        ;;
esac
AC_SUBST(LIBS_ODBC)
AC_SUBST(CPPFLAGS_ODBC)


#for SMTPAppender
AC_MSG_CHECKING(for SMTP support)
AC_ARG_WITH(SMTP,
        AC_HELP_STRING(--with-SMTP, [SMTP support. Accepted arguments :
                libesmtp, no (default=no)]),
        [ac_with_smtp=$withval],
        [ac_with_smtp=no])
case "$ac_with_smtp" in
    libesmtp)
        AC_MSG_RESULT(libesmtp)
        AC_CHECK_LIB([esmtp], [smtp_create_session],,
                AC_MSG_ERROR(libesmtp library not found !),
                -lesmtp)
        AC_SUBST(HAS_LIBESMTP, 1, SMTP support through libesmtp library.)
        LIBS="-lesmtp $LIBS"
        ;;
        no)
        AC_MSG_RESULT(no)
        AC_SUBST(HAS_LIBESMTP, 0, SMTP support through libesmtp library.)
        ;;
    *)
        AC_MSG_RESULT(???)
        AC_MSG_ERROR(Unknown option : $ac_with_smtp)
        ;;
esac

#for char api
AC_ARG_ENABLE(char,
        AC_HELP_STRING(--enable-char,
	        [enable char API (yes)]))
if test "x$enable_char" = xno; then
        have_char_api=no
else
        have_char_api=yes
fi

if test "$have_char_api" = "yes"
then
    AC_SUBST(CHAR_API, 1)
else
    AC_SUBST(CHAR_API, 0)
fi

#for wchar_t api
AC_ARG_ENABLE(wchar_t,
        AC_HELP_STRING(--enable-wchar_t,
	        [enable wchar_t API (yes if wchar_t available)]))
if test "x$enable_wchar_t" = xno; then
        have_wchar_t_api=no
else
    if test "$have_wchar_t" = "yes"
    then
        have_wchar_t_api=yes
    else
        have_wchar_t_api=no
    fi
fi

if test "$have_wchar_t_api" = "yes"
then
    AC_SUBST(WCHAR_T_API, 1)
else
    AC_SUBST(WCHAR_T_API, 0)
fi

#for unichar api
AC_ARG_ENABLE(unichar,
        AC_HELP_STRING(--enable-unichar,
	        [enable unichar API (no)]))
if test "x$enable_unichar" = "yes"
then
    AC_SUBST(UNICHAR_API, 1)
else
    AC_SUBST(UNICHAR_API, 0)
fi

#for cfstring api
AC_ARG_ENABLE(cfstring,
        AC_HELP_STRING(--enable-cfstring,
	        [enable cfstring API (no)]))
if test "x$enable_cfstring" = xyes; then
        AC_MSG_CHECKING([for cfstring])
        CPPFLAGS="$CPPFLAGS -framework CoreFoundation"
        AC_COMPILE_IFELSE([AC_LANG_SOURCE([
                   #include <CoreFoundation/CFString.h>
                   CFStringRef x = CFSTR("Hello");])], [have_cfstring_api=yes], [have_cfstring_api=no])
        AC_MSG_RESULT($have_cfstring_api)
else
        have_cfstring_api=no
fi

if test "$have_cfstring_api" = "yes"
then
    AC_SUBST(CFSTRING_API, 1)
else
    AC_SUBST(CFSTRING_API, 0)
fi

#determine charset type
AC_MSG_CHECKING([charset type])
AC_ARG_WITH(charset,
        AC_HELP_STRING(--with-charset=TYPE, [locale charset.
                Accepted TYPE variants: auto, utf-8, iso-8859-1, usascii, ebcdic (default=auto)]),
        [ac_with_charset=$withval],
        [ac_with_charset=auto])

case "$ac_with_charset" in
    utf-8)
        AC_MSG_RESULT(utf-8)
        AC_SUBST(CHARSET_UTF8, 1)
        AC_SUBST(CHARSET_ISO88591, 0)
        AC_SUBST(CHARSET_USASCII, 0)
        AC_SUBST(CHARSET_EBCDIC, 0)
        ;;

    iso-8859-1)
        AC_MSG_RESULT(iso-8859-1)
        AC_SUBST(CHARSET_UTF8, 0)
        AC_SUBST(CHARSET_ISO88591, 1)
        AC_SUBST(CHARSET_USASCII, 0)
        AC_SUBST(CHARSET_EBCDIC, 0)
        ;;

    usascii)
        AC_MSG_RESULT(usascii)
        AC_SUBST(CHARSET_UTF8, 0)
        AC_SUBST(CHARSET_ISO88591, 0)
        AC_SUBST(CHARSET_USASCII, 1)
        AC_SUBST(CHARSET_EBCDIC, 0)
        ;;

    ebcdic)
        AC_MSG_RESULT(usascii)
        AC_SUBST(CHARSET_UTF8, 0)
        AC_SUBST(CHARSET_ISO88591, 0)
        AC_SUBST(CHARSET_USASCII, 0)
        AC_SUBST(CHARSET_EBCDIC, 1)
        ;;

    auto)
        AC_MSG_RESULT(auto)
        AC_SUBST(CHARSET_UTF8, 0)
        AC_SUBST(CHARSET_ISO88591, 0)
        AC_SUBST(CHARSET_USASCII, 0)
        AC_SUBST(CHARSET_EBCDIC, 0)
        ;;

    *)
        AC_MSG_RESULT(???)
        AC_MSG_ERROR(Invalid charset type: $ac_with_charset)
        ;;
esac



#determine logchar type
AC_MSG_CHECKING([logchar type])
AC_ARG_WITH(logchar,
        AC_HELP_STRING(--with-logchar=TYPE, [type for logchar.
                Accepted TYPE variants: utf-8, wchar_t, unichar (default=utf-8)]),
        [ac_with_logchar=$withval],
        [ac_with_logchar=utf-8])

case "$ac_with_logchar" in
    utf-8)
        AC_MSG_RESULT(utf-8)
        AC_SUBST(LOGCHAR_IS_UTF8, 1)
        AC_SUBST(LOGCHAR_IS_WCHAR, 0)
        AC_SUBST(LOGCHAR_IS_UNICHAR, 0)
        ;;

    wchar_t)
        AC_MSG_RESULT(wchar_t)
        AC_SUBST(LOGCHAR_IS_UTF8, 0)
        AC_SUBST(LOGCHAR_IS_WCHAR, 1)
        AC_SUBST(LOGCHAR_IS_UNICHAR, 0)
        ;;

    unichar)
        AC_MSG_RESULT(unichar)
        AC_SUBST(LOGCHAR_IS_UTF8, 0)
        AC_SUBST(LOGCHAR_IS_WCHAR, 0)
        AC_SUBST(LOGCHAR_IS_UNICHAR, 1)
        ;;

    *)
        AC_MSG_RESULT(???)
        AC_MSG_ERROR(Invalid logchar type: $ac_with_logchar)
        ;;
esac



# Create files
# ----------------------------------------------------------------------------

AC_CONFIG_FILES([
Makefile
liblog4cxx.pc
src/Makefile
src/main/Makefile
src/main/cpp/Makefile
src/main/include/Makefile
src/main/include/log4cxx/log4cxx.h
src/main/include/log4cxx/private/log4cxx_private.h
src/main/include/log4cxx/private/Makefile
src/main/include/log4cxx/Makefile
src/main/include/log4cxx/helpers/Makefile
src/main/include/log4cxx/net/Makefile
src/main/include/log4cxx/nt/Makefile
src/main/include/log4cxx/spi/Makefile
src/main/include/log4cxx/spi/location/Makefile
src/main/include/log4cxx/varia/Makefile
src/main/include/log4cxx/xml/Makefile
src/main/include/log4cxx/config/Makefile
src/main/include/log4cxx/db/Makefile
src/main/include/log4cxx/rolling/Makefile
src/main/include/log4cxx/pattern/Makefile
src/main/include/log4cxx/filter/Makefile
src/m4/Makefile
src/site/Makefile
src/site/doxy/Makefile
src/site/doxy/Doxyfile
src/test/Makefile
src/test/resources/Makefile
src/test/resources/input/Makefile
src/test/resources/input/ndc/Makefile
src/test/resources/input/rolling/Makefile
src/test/resources/input/xml/Makefile
src/test/cpp/Makefile
src/test/resources/witness/Makefile
src/test/resources/witness/encoding/Makefile
src/test/resources/witness/ndc/Makefile
src/test/resources/witness/rolling/Makefile
src/examples/Makefile
src/examples/cpp/Makefile
])
AC_OUTPUT
